// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
  _before: function () {
    // 通用预处理器
  },
  async bannerAdd(params = {}) {
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    return await dbJQL.collection('dc-activity-banner').add(params);
  },
  async bannerList() {
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    let bannerTemp = await dbJQL
      .collection('dc-activity-banner')
      .orderBy('sort asc')
      .getTemp();
    let userTemp = await dbJQL
      .collection('uni-id-users')
      .field('_id,nickname')
      .getTemp();
    let res = await dbJQL
      .collection(bannerTemp, userTemp)
      .field(
        'appid,checked,createTime,picurl,sort,target,url,user_id.nickname as nickname'
      )
      .get();
    let data = res.data.map((item) => {
      return {
        ...item,
        nickname: item.nickname[0],
      };
    });
    return { ...res, data };
  },
  async bannerRemove(ids = []) {
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });
    return await dbJQL
      .collection('dc-activity-banner')
      .where(`_id in ${JSON.stringify(ids)}`)
      .remove();
  },

  async bannerUpdate(params={}){
	let {_id,...data} = params;
	const dbJQL = uniCloud.databaseForJQL({
		clientInfo:this.getClientInfo()
	})
	return await dbJQL.collection("dc-activity-banner").doc(_id)
	.update({...data})
}	
};
